package com.toocms.jinjuhang.ui.index.article_details

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.system.NewsDetailData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.share.TabShare
import com.umeng.socialize.bean.SHARE_MEDIA

class ArticleDetailsModel(application: Application, val bundle: Bundle?) :
    BaseViewModel<BaseModel>(application) {
    val type by lazy {
        bundle?.getString(Constants.BundleKey.KEY_TYPE, Constants.Extra.NEWS_TYPE_NEWS)
            ?: Constants.Extra.NEWS_TYPE_NEWS
    }

    val newsId by lazy {
        bundle?.getString(Constants.BundleKey.KEY_NEWS_ID, "") ?: ""
    }

    val dataObservableField = ObservableField<NewsDetailData>()

    val recommendCommodityItems = ObservableArrayList<ArticleDetailsRecommendCommodityItemModel>()
    val recommendCommodityItemBinding = ItemBinding.of<ArticleDetailsRecommendCommodityItemModel>(
        BR.articleDetailsRecommendCommodityItemModel,
        R.layout.item_article_recommend_commodity
    )

    val shareClickBindingCommand=BindingCommand<BindingAction>(BindingAction {
        dataObservableField.get()?.apply {
            TabShare.getOneKeyShare()
                .setPlatform(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                .setMin(
                    "https://www.baidu.com/",
                    title,
                    author,
                    "pages/lanhu_111shouye/index?news_id=${newsId}&member_sn=${UserRepository.user.member_sn}",
                    Constants.ThirdPartyInfo.WECHAT_ORIGINAL_ID,
                    R.mipmap.icon_logo
                )
                .share()
        }

    })


    init {
        val url:String
        val params = mutableMapOf<String, String?>()
        when (type) {
            Constants.Extra.NEWS_TYPE_NEWS -> {
                url = Urls.System.NEWS_DETAIL
                params["news_id"] = newsId
            }
            Constants.Extra.NEWS_TYPE_ACTIVE -> {
                url = Urls.System.ACTART_DETAIL
                params["aa_id"] = newsId
            }
            else -> {
                url = Urls.System.NEWS_DETAIL
                params["news_id"] = newsId
            }
        }
        requestData(
            url,
            params,
            resultClass = NewsDetailData::class.java,
            result = {
                dataObservableField.set(it)
                recommendCommodityItems.clear()
                if(null!=it.rescue && !it.rescue.goods_id.isNullOrEmpty()){
                    recommendCommodityItems += ArticleDetailsRecommendCommodityItemModel(
                        this,
                        ObservableField(it.rescue)
                    )
                }

            })


    }

    override fun onStop() {
        super.onStop()
        TabShare.release()
    }

}